package com.ruoyi.web.controller.mdata;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.data.domain.Merchant;
import com.ruoyi.data.service.IMerchantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 美团外卖商户Controller
 * 
 * @author ruoyi
 * @date 2019-09-24
 */
@Controller
@RequestMapping("/data/mtwm")
public class MerchantController extends BaseController
{
    private String prefix = "/data/mtwm";

    @Autowired
    private IMerchantService merchantService;

    @RequiresPermissions("data:mtwm:view")
    @GetMapping()
    public String merchant()
    {
        return prefix + "/merchant";
    }

    /**
     * 查询美团外卖商户列表
     */
    //@RequiresPermissions("data:mtwm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Merchant merchant)
    {
        //startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<Merchant> list = merchantService.selectMerchantList(merchant,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(1000);
        return rspData;
    }

    /**
     * 导出美团外卖商户列表
     */
    @RequiresPermissions("data:mtwm:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Merchant merchant)
    {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        return util.exportExcel(list, "merchant");
    }

    /**
     * 新增美团外卖商户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存美团外卖商户
     */
    @RequiresPermissions("data:mtwm:add")
    @Log(title = "美团外卖商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Merchant merchant)
    {
        return toAjax(merchantService.insertMerchant(merchant));
    }

    /**
     * 修改美团外卖商户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Merchant merchant = merchantService.selectMerchantById(id);
        mmap.put("merchant", merchant);
        return prefix + "/edit";
    }

    /**
     * 修改保存美团外卖商户
     */
    @RequiresPermissions("data:mtwm:edit")
    @Log(title = "美团外卖商户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Merchant merchant)
    {
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除美团外卖商户
     */
    @RequiresPermissions("data:mtwm:remove")
    @Log(title = "美团外卖商户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(merchantService.deleteMerchantByIds(ids));
    }
}
